package com.example.phonerefresh.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.phonerefresh.R;
import com.example.phonerefresh.view.PullToRefreshView;

import java.util.Map;

/**
 * Created by l on 2016/11/29.
 */

public class FoundFragment extends BaseFragment {

    private PullToRefreshView mPullToRefreshView;

    public FoundFragment(){

    }

    public FoundFragment newInstance() {
        FoundFragment fragment = new FoundFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_recycler_view, container, false);

        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        RefreshAdapter mAdapter = new RefreshAdapter();
        recyclerView.setAdapter(mAdapter);

        mPullToRefreshView = (PullToRefreshView)view.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);
            }
        });

        return view;
    }

    public class RefreshAdapter extends RecyclerView.Adapter<RefreshAdapter.SampleHolder>{

        @Override
        public RefreshAdapter.SampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
            return new SampleHolder(view);
        }

        @Override
        public void onBindViewHolder(SampleHolder holder, int position) {
            Map<String,Integer> data = mSampleList.get(position);
            holder.bindData(data);
        }

        @Override
        public int getItemCount() {
            return mSampleList.size();
        }

        public class SampleHolder extends RecyclerView.ViewHolder{

            ImageView mImageView;
            View rootView;

            Map<String,Integer> mData;

            public SampleHolder(View itemView) {
                super(itemView);

                rootView = itemView;
                mImageView = (ImageView)itemView.findViewById(R.id.image_view_icon);
            }

            public void bindData(Map<String, Integer> data) {
                mData = data;

                rootView.setBackgroundResource(mData.get(KEY_COLOR));
                mImageView.setImageResource(mData.get(KEY_ICON));
            }
        }
    }
}
